package com.wutianyi.module.commons;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wutianyi 属性组
 */
public class PropertyGroup {
	
	/**
	 * 
	 */
	private int id;
	/**
	 * 以来的配置组的id
	 */
	private int cId;
	private int parentId;
	private String name;
	private String description;
	private int order;
	private PropertyGroupType type;
	
	List<Item> items = new ArrayList<Item>();
	
	Map<Integer, Item> mapItems;
	
	private Date gmtCreate;
	private Date gmtModified;
	
	public PropertyGroup(int parentId, int cId, String name, String value, String description,
			PropertyGroupType type, Map<String, String> properties) {
		this.parentId = parentId;
		this.cId = cId;
		this.name = name;
		this.description = description;
		this.type = type;
		if (null != properties && properties.size() > 0) {
			setPrimary(name, value, description, properties);
		} else {
			setPrimary(name, value, description);
		}
	}
	
	public PropertyGroup() {
		
	}
	
	public void setPrimary(String name, String value, String description) {
		Item item = new Item(id, name, value, description, 1);
		items.add(item);
	}
	
	public void setPrimary(String name, String value, String description,
			Map<String, String> properties) {
		Item item = new Item(id, name, value, description, 1, properties);
		items.add(item);
	}
	
	public void addItem(String name, String value, String description) {
		items.add(new Item(id, name, value, description, 1));
	}
	
	public void addItem(String name, String value, String description,
			Map<String, String> properties) {
		items.add(new Item(id, name, value, description, order, properties));
	}
	
	public void addItem(Item item) {
		if (null == mapItems) {
			mapItems = new HashMap<Integer, Item>();
		}
		items.add(item);
		mapItems.put(item.getId(), item);
	}
	
	public Item getItem(int itemId) {
		if (null == mapItems) {
			return null;
		}
		return mapItems.get(itemId);
	}
	
	public int getId() {
		return id;
	}
	
	public void setId(int id) {
		this.id = id;
	}
	
	public int getParentId() {
		return parentId;
	}
	
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	
	public String getName() {
		return name;
	}
	
	public void setName(String name) {
		this.name = name;
	}
	
	public String getDescription() {
		return description;
	}
	
	public void setDescription(String description) {
		this.description = description;
	}
	
	public PropertyGroupType getType() {
		return type;
	}
	
	public void setType(PropertyGroupType type) {
		this.type = type;
	}
	
	public List<Item> getItems() {
		return items;
	}
	
	public void setItems(List<Item> items) {
		this.items = items;
	}
	
	public Date getGmtCreate() {
		return gmtCreate;
	}
	
	public void setGmtCreate(Date gmtCreate) {
		this.gmtCreate = gmtCreate;
	}
	
	public Date getGmtModified() {
		return gmtModified;
	}
	
	public void setGmtModified(Date gmtModified) {
		this.gmtModified = gmtModified;
	}
	
	public int getOrder() {
		return order;
	}
	
	public void setOrder(int order) {
		this.order = order;
	}
	
	public String toString() {
		StringBuilder builder = new StringBuilder();
		builder.append("[\n\tid: ");
		builder.append(id);
		builder.append("\n\tcId: ");
		builder.append(cId);
		builder.append("\n\tparentId: ");
		builder.append(parentId);
		builder.append("\n\tname: ");
		builder.append(name);
		builder.append("\n\tdescription: ");
		builder.append(description);
		builder.append("\n\ttype: ");
		builder.append(type);
		builder.append("\n\tgmtCreate: ");
		builder.append(gmtCreate);
		builder.append("\n\tgmtModified: ");
		builder.append(gmtModified);
		if (null != items && !items.isEmpty()) {
			builder.append("\n\titems:\n\t{");
			for (Item item : items) {
				builder.append(item);
			}
			builder.append("\n\t}");
		}
		return builder.toString();
	}
	
	public int getcId() {
		return cId;
	}
	
	public void setcId(int cId) {
		this.cId = cId;
	}
	
}
